/*
 * utility functions
 * */

function moveTo(theURL){
    window.location = theURL;
}

function isAlpha(aChar){
    return (aChar >= 'a' && aChar <= 'z') || (aChar >= 'A' && aChar <= 'Z');
}

function isDigit(charCode){
    return charCode >= 0 && charCode <= 9;
}

function isInteger(num){
    return !isNaN(num) && num.indexOf('.') == -1;
}

function isDecimal(num){
    return ((false == isNaN(num)) && num.indexOf('.') != -1);
}

function isNumber(num){
    return (isDecimal(num) || isInteger(num));
}

/*
 * field validation functions
 * */

function fieldFilled(field){
    with (field){
        if (value == null || value == ""){
            return false;
        }
        return true;
    }
}

function validEmail(field, alertTxt){
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    
    with (field){
        if(false == filter.test(value)){
            alert(alertTxt);
            field.focus();
            return false;
        }
        return true;
    }
}

function validDOB(field){
    // Checks for the following valid date formats:
    // MM/DD/YY   MM/DD/YYYY   MM-DD-YY   YY-DD-YYYY
    // Also separates date into month, day, and year variables

    // To require a 4 digit year entry, use this line instead:
    var datePat = /^(\d{4})(\-)(\d{2})(\-)(\d{2})$/;

    var matchArray = field.value.match(datePat); // is the format ok?
    
    if (matchArray == null) {
        alert("Date of Birth is not in a valid format.");
        return false;
    }
    year = matchArray[1]; // parse date into variables
    month = matchArray[3];
    day = matchArray[5];
    
    if (month < 1 || month > 12) { // check month range
        alert("Month must be between 1 and 12.");
        return false;
    }
    if (day < 1 || day > 31) {
        alert("Day must be between 1 and 31.");
        return false;
    }
    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        alert("Month "+month+" doesn't have 31 days!");
        return false;
    }
    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day>29 || (day==29 && !isleap)) {
            alert("February " + year + " doesn't have " + day + " days!");
            return false;
       }
    }
    return true;  // date is valid
}

function validSSN(field){
    // Checks for the following valid date formats:
    // MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
    // Also separates date into month, day, and year variables

    // To require a 4 digit year entry, use this line instead:
    var ssnPat = /^(\d{1})(\-)(\d{2})(\-)(\d{3})$/;

    var matchArray = field.value.match(ssnPat); // is the format ok?
    
    if (matchArray == null) {
        alert("SSN is not in a valid format.");
        return false;
    }
    return true;  // date is valid
}

function validYear(field, alertTxt){
    with (field){
        if( true == isNaN(value)){
            alert(alertTxt);
            return false;
        }
        
        if( (4 != value.length) || (1900 > value)){
            alert(alertTxt);
            return false;
        }
        return true;
    }
}

function validCurrency(field, alertTxt){
    with (field){
        if( true == isNaN(value) || (0 > value)){
            alert(alertTxt);
            return false;
        }
        value = formatCurrancy(value);
    }
    return true;
}

function validNum(field, alertTxt){
    if(true == isNaN(parseInt(field.value))){
        alert(alertTxt);
        return false;
    }
    
    return true;
}

function validateLoginForm(thisForm){
 with(thisForm){
    b = true;
    str = "Please fill in the following Field(s):\n";
    
    if(false == fieldFilled(email)){
        b = false;
    }
    else if(false == validEmail(email, "Please enter a valid email address.")){
        return false;
    }
    if(false == fieldFilled(password, 'You must enter a password.')){
        password.focus();
        b = false;
    }
    return b;
    }
}

function validateChangePasswordForm(thisForm){
 with(thisForm){
    b = true;
    str = "Please fill in the following Field(s):\n";
    count = 1;
    if(false == fieldFilled(password)){
        str += count++ + ". Password\n";
        b = false;
    }
    if(false == fieldFilled(confirmPassword)){
        str += count++ + ". Confirm Password\n";
        b = false;
    }
    if(password.value != confirmPassword.value){
        alert("Your password and password confirmation must match.");
        return false;
    } 
    else if(password.value.length < 8) {
        alert("Your password must be at least 8 characters long.");
        return false;
    }

    return b;
    }
}

/*
 * form validation functions
 * */

function validateForm(thisForm){
    with (thisForm){
        b = true;
        str = "Please fill in the following Field(s):\n";
        count = 1;
        if (false == fieldFilled(firstname)){
            str += count++ + ". First Name\n";
            b = false;
        }
        else if(firstname.value.length > 32){
            alert("Sorry, we cannot store more than 32 characters for your first name.");
            return false;
        }
        if (false == fieldFilled(lastname)){
            str += count++ + ". Last Name\n";
            b = false;
        }
        else if(lastname.value.length > 64){
            alert("Sorry, we cannot store more than 64 characters for your last name.");
            return false;
        }
        if(false == fieldFilled(email)){
            str += count++ + ". Email\n";
            b = false;
        }
        else if(false == validEmail(email, "Please enter a valid email address.")){
            return false;
        }
        else if(email.value.length > 64){
            alert("Sorry, we cannot store more than 64 characters for your email. Please choose an alternative email address.");
            return false;
        }
        if(false == fieldFilled(address)){
            str += count++ + ". Address\n";
            b = false;
        }
        else if(address.value.length > 64){
            alert("Sorry, we cannot store more than 64 characters for your address.");
            return false;
        }
        if(false == fieldFilled(city)){
            str += count++ + ". City\n";
            b = false;
        }
        else if(city.value.length > 32){
            alert("Sorry, we cannot store more than 32 characters for your city.");
            return false;
        }
        if(false == fieldFilled(state)){
            str += count++ + ". State\n";
            b = false;
        }            
        if(false == fieldFilled(dob)){
            str += count++ + ". Date of Birth\n";
            b = false;
        }
        else if(false == validDOB(dob)){
            return false;
        }
        if(false == fieldFilled(ssn)){
            str += count++ + ". Social Security Number\n";
            b = false;
        }
        else if(false == validSSN(ssn)){
            return false;
        }
        if(false == fieldFilled(passwordhash)){
            str += count++ + ". Password\n";
            b = false;
        }
        if(false == fieldFilled(confirmPassword)){
            str += count++ + ". Confirmed Password\n";
            b = false;
        }
        if(passwordhash.value != confirmPassword.value){
            alert("Your password and password confirmation must match.");
            return false;
        } 
        else if(passwordhash.value.length < 8) {
            alert("Your password must be at least 8 characters long.");
            return false;
        }
        if(false == fieldFilled(question)){
            str += count++ + ". Security Question\n";
            b = false;
        }
        else if(question.value.length > 128){
            alert("Sorry, we cannot store more than 128 characters for your question.");
            return false;
        }
        if(false == fieldFilled(answerhash)){
            str += count++ + ". Security Question Answer\n";
            b = false;
        }

        if(false == b){
            alert(str);
            return false;
        }
        return true;
    }
}


function validateEditFormManager(thisForm){
    with (thisForm){
        b = true;
        str = "Please fill in the following Field(s):\n";
        count = 1;
        if (false == fieldFilled(firstname)){
            str += count++ + ". First Name\n";
            b = false;
        }
        else if(firstname.value.length > 32){
            alert("Sorry, we cannot store more than 32 characters for your first name.");
            return false;
        }
        if (false == fieldFilled(lastname)){
            str += count++ + ". Last Name\n";
            b = false;
        }
        else if(lastname.value.length > 64){
            alert("Sorry, we cannot store more than 64 characters for your last name.");
            return false;
        }
        if(false == fieldFilled(email)){
            str += count++ + ". Email\n";
            b = false;
        }
        else if(false == validEmail(email, "Please enter a valid email address.")){
            return false;
        }
        else if(email.value.length > 64){
            alert("Sorry, we cannot store more than 64 characters for your email. Please choose an alternative email address.");
            return false;
        }
        if(false == fieldFilled(address)){
            str += count++ + ". Address\n";
            b = false;
        }
        else if(address.value.length > 64){
            alert("Sorry, we cannot store more than 64 characters for your address.");
            return false;
        }
        if(false == fieldFilled(city)){
            str += count++ + ". City\n";
            b = false;
        }
        else if(city.value.length > 32){
            alert("Sorry, we cannot store more than 32 characters for your city.");
            return false;
        }
        if(false == fieldFilled(state)){
            str += count++ + ". State\n";
            b = false;
        }            
        if(false == fieldFilled(dob)){
            str += count++ + ". Date of Birth\n";
            b = false;
        }
        else if(false == validDOB(dob)){
            return false;
        }
        if(false == fieldFilled(ssn)){
            str += count++ + ". Social Security Number\n";
            b = false;
        }
        else if(false == validSSN(ssn)){
            return false;
        }
        if(false == b){
            alert(str);
            return false;
        }
        return true;
    }
}

function validateEditForm(thisForm){
    with (thisForm){
        b = true;
        str = "Please fill in the following Field(s):\n";
        count = 1;
        if (false == fieldFilled(firstname)){
            str += count++ + ". First Name\n";
            b = false;
        }
        else if(firstname.value.length > 32){
            alert("Sorry, we cannot store more than 32 characters for your first name.");
            return false;
        }
        if (false == fieldFilled(lastname)){
            str += count++ + ". Last Name\n";
            b = false;
        }
        else if(lastname.value.length > 64){
            alert("Sorry, we cannot store more than 64 characters for your last name.");
            return false;
        }
        if(false == fieldFilled(email)){
            str += count++ + ". Email\n";
            b = false;
        }
        else if(false == validEmail(email, "Please enter a valid email address.")){
            return false;
        }
        else if(email.value.length > 64){
            alert("Sorry, we cannot store more than 64 characters for your email. Please choose an alternative email address.");
            return false;
        }
        if(false == fieldFilled(address)){
            str += count++ + ". Address\n";
            b = false;
        }
        else if(address.value.length > 64){
            alert("Sorry, we cannot store more than 64 characters for your address.");
            return false;
        }
        if(false == fieldFilled(city)){
            str += count++ + ". City\n";
            b = false;
        }
        else if(city.value.length > 32){
            alert("Sorry, we cannot store more than 32 characters for your city.");
            return false;
        }
        if(false == fieldFilled(state)){
            str += count++ + ". State\n";
            b = false;
        }            
        if(false == fieldFilled(dob)){
            str += count++ + ". Date of Birth\n";
            b = false;
        }
        else if(false == validDOB(dob)){
            return false;
        }
        if(false == fieldFilled(ssn)){
            str += count++ + ". Social Security Number\n";
            b = false;
        }
        else if(false == validSSN(ssn)){
            return false;
        }
        if(true == fieldFilled(passwordhash)){
            if(passwordhash.value.length < 8) {
                alert("Your password must be at least 8 characters long.");
                return false;
            }
        } // If it's not in here assume it's not going to be changed 

        if(false == fieldFilled(question)){
            str += count++ + ". Security Question\n";
            b = false;
        }
        else if(question.length > 128){
            alert("Sorry, we cannot store more than 128 characters for your question.");
            return false;
        }

        if(false == b){
            alert(str);
            return false;
        }
        return true;
    }
}

function validateWithdrawalForm(thisForm){
    with (thisForm){
        b = true;
        if(false == fieldFilled(aid)){
            alert("You must activate an account before this transaction can happen.");
            return false;
        }
        
        // accountID-balance
        var accountRegEx = /^[0-9]+\-([0-9]+(\.\d{1,2})?)$/;
        var amountRegEx = /^[+]?[0-9]+(\.\d{1,2})?$/;

        var amtMatchArray       = amount.value.match(amountRegEx); 
        var amtFromMatchArray   = aid.value.match(accountRegEx);  

        if (amtMatchArray == null) {
            alert("Please enter a postive numeric value to withdraw.");
            return false;
        }
        amt = amtMatchArray[0];
        amtFrom = amtFromMatchArray[1];
        
        if(amt > (amtFrom - 25)){
            alert("You can only withdraw less than the balance minus the minimum required balance ($25.00).");
            return false;
        }
        
        return true;
    }
}

function validateDepositForm(thisForm){
    with (thisForm){
        b = true;
        if(false == fieldFilled(aid)){
            alert("You must activate an account before this transaction can happen.");
            return false;
        }
        
        // accountID-balance
        var amountRegEx = /^[+]?[0-9]+(\.\d{1,2})?$/;

        var amtMatchArray       = amount.value.match(amountRegEx); 

        if (amtMatchArray == null) {
            alert("Please enter a postive numeric value to withdraw.");
            return false;
        }        
        return true;
    }
}

function validateTransferForm(thisForm){
    with (thisForm){
        b = true;
        if(false == fieldFilled(aidFrom)){
            alert("You must activate an account before this transaction can happen.");
            return false;
        }
        if(aidTo.value == aidFrom.value){
            alert("You must select different accounts to transfer money between.");
            return false;
        } 

        // accountID-balance
        var accountRegEx = /^[0-9]+\-([0-9]+(\.\d{1,2})?)$/;
        var amountRegEx = /^[+]?[0-9]+(\.\d{1,2})?$/;

        var amtMatchArray       = amount.value.match(amountRegEx); 
        var amtFromMatchArray   = aidFrom.value.match(accountRegEx);  

        if (amtMatchArray == null) {
            alert("Please enter a postive numeric value to transfer.");
            return false;
        }
        amt = amtMatchArray[0];
        amtFrom = amtFromMatchArray[1];
        
        if(amt > (amtFrom - 25)){
            alert("You can only transfer less than the balance minus the minimum required balance ($25.00).");
            return false;
        }

        return true;
    }
}